#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>

char* newnew = (char*)"HELLO=99";

char* const addenv[] = 
{
    (char* const)"MYENV=123",
    (char* const)"HELLO=666",
    NULL
};


int main()
{
    printf("我的程序开始运行\n");
    if(fork() == 0)
    {
        printf("子进程的pid->%d\n", getpid());
        //execl("/usr/bin/ls","ls","-l","-a",NULL);
        //execl("./other","other",NULL);
        //execlp("ls","-l","-a",NULL);

        char* const argv[] = 
        {
            //(char* const)"ls",
            (char* const)"other",
            (char* const)"-a",
           //(char* const)"-b",
            (char* const)"-l",
            NULL
        };
        
        //execv("/usr/bin/ls",argv);
        //execvp(argv[0],argv);
        //添加环境变量
        for(int i = 0;addenv[i];i++)
        {
            putenv(addenv[i]);
        }

        //putenv(newnew);


        //当前的环境变量
        extern char** environ; 
        execvpe("./other",argv,environ);

        exit(1);
    }
    //execl("/usr/bin/ls","ls","-l","-a",NULL);
    //execl("/usr/bin/top","top",NULL);


    pid_t rid = waitpid(-1, NULL, 0);
    printf("回收的进程pid->%d\n", rid);
    printf("我的程序运行结束\n");
    return 0;
}
